import tkinter as tk
from tkinter import ttk
import tkinter.messagebox
from StudentManager import StudentManager
import redis

class LoginPage(object):
    def __init__(self, master=None):
        self.root = master
        self.root.title('教师管理系统')
        # 调整窗口大小和位置
        self.root.geometry('600x450+450+250')
        self.root.configure(bg='#121212') # 更深的背景色
        
        ## 设置窗口样式 (ttk样式在这里可能不完全生效，我们主要通过参数控制)
        # self.style = ttk.Style()
        # self.style.configure('Custom.TButton', 
        #                    font=('微软雅黑', 12),
        #                    padding=10)
        
        self.username = tk.StringVar()
        self.password = tk.StringVar()
        self.create_page()

    def create_page(self):
        # 创建主框架
        main_frame = tk.Frame(self.root, bg='#121212')
        main_frame.pack(expand=True, fill='both', padx=30, pady=30)
        
        # 标题区域
        title_frame = tk.Frame(main_frame, bg='#121212')
        title_frame.pack(pady=20)
        
        title_label = tk.Label(title_frame, 
                             text='教师管理系统',
                             font=('微软雅黑', 32, 'bold'), # 增加字号
                             bg='#121212',
                             fg='#00bcd4') # 亮青色
        title_label.pack()
        
        subtitle_label = tk.Label(title_frame,
                                text='TEACHER DASHBOARD',
                                font=('Consolas', 14), # 使用 Consolas 字体增加科技感
                                bg='#121212',
                                fg='#607d8b') # 稍暗的文字颜色
        subtitle_label.pack(pady=5)
        
        # 登录框框架
        login_frame = tk.Frame(main_frame, bg='#1e1e1e', bd=2, relief='flat', highlightthickness=1, highlightbackground='#37474f') # 深灰色背景，带边框
        login_frame.pack(pady=30, padx=50, fill='x')
        
        # 用户名输入框
        username_frame = tk.Frame(login_frame, bg='#1e1e1e')
        username_frame.pack(fill='x', pady=10, padx=20)
        tk.Label(username_frame, 
                text='账 户',
                font=('微软雅黑', 14, 'bold'),
                bg='#1e1e1e',
                fg='#b0bec5').pack(side='left', padx=10) # 浅灰色文字
        username_entry = tk.Entry(username_frame,
                                textvariable=self.username,
                                font=('Consolas', 14),
                                width=25,
                                bg='#263238', # 更深的输入框背景
                                fg='#00bcd4', # 亮青色文字
                                insertbackground='#00bcd4', # 光标颜色
                                relief='flat',
                                highlightthickness=1,
                                highlightbackground='#37474f', # 非焦点边框色
                                highlightcolor='#00bcd4') # 焦点边框色
        username_entry.pack(side='right', padx=10)
        
        # 密码输入框
        password_frame = tk.Frame(login_frame, bg='#1e1e1e')
        password_frame.pack(fill='x', pady=10, padx=20)
        tk.Label(password_frame,
                text='密 码',
                font=('微软雅黑', 14, 'bold'),
                bg='#1e1e1e',
                fg='#b0bec5').pack(side='left', padx=10)
        password_entry = tk.Entry(password_frame,
                                textvariable=self.password,
                                show='●',
                                font=('Consolas', 14),
                                width=25,
                                bg='#263238',
                                fg='#00bcd4',
                                insertbackground='#00bcd4',
                                relief='flat',
                                highlightthickness=1,
                                highlightbackground='#37474f',
                                highlightcolor='#00bcd4')
        password_entry.pack(side='right', padx=10)
        
        # 按钮框架
        button_frame = tk.Frame(main_frame, bg='#121212')
        button_frame.pack(pady=20)
        
        # 登录按钮
        login_button = tk.Button(button_frame,
                               text='登 录',
                               font=('微软雅黑', 14, 'bold'),
                               bg='#00bcd4', # 亮青色背景
                               fg='#121212', # 深色文字
                               activebackground='#00838f', # 点击时的背景色
                               activeforeground='#ffffff', # 点击时的文字颜色
                               width=15,
                               height=1, # 增加高度
                               relief='flat',
                               command=self.login_check)
        login_button.pack(side='left', padx=15) # 增加按钮间距
        
        # 退出按钮
        quit_button = tk.Button(button_frame,
                              text='退 出',
                              font=('微软雅黑', 14, 'bold'),
                              bg='#e91e63', # 亮品红色背景
                              fg='#121212', # 深色文字
                              activebackground='#c2185b',
                              activeforeground='#ffffff',
                              width=15,
                              height=1,
                              relief='flat',
                              command=self.root.quit)
        quit_button.pack(side='left', padx=15)

    def login_check(self):
        name = self.username.get()
        secret = self.password.get()
        # 连接 Redis 数据库 (注意：这里每次检查都连接一次，实际应用中应优化)
        db2 = redis.Redis(host='127.0.0.1', port=6379, db=2, decode_responses=True)
        if name == 'root' and secret == '123456':
            self.root.destroy()
            # 在新的顶级窗口中启动 StudentManager
            sm_root = tk.Tk()
            StudentManager(sm_root)
            sm_root.mainloop()
        elif name in db2.keys():
            mi = db2.get(name)
            if secret == mi:
                self.root.destroy()
                # Note: StudentOnly is the student side, which user requested to remove.
                # We should not instantiate StudentOnly here based on user's request.
                # If the user logs in with a student account, show an error or disable access.
                # For now, let's assume only the teacher ('root') should log in.
                tkinter.messagebox.showerror('错误', '学生端功能已禁用，请使用教师账号登录！')
            else:
                tkinter.messagebox.showerror('错误', '账号或密码错误！')
        else:
            tkinter.messagebox.showerror('错误', '账号或密码错误！')

if __name__ == '__main__':
    root = tk.Tk()
    LoginPage(root)
    root.mainloop()
